import datetime
import pandas as pd
from absl import app, flags
from sqlalchemy.orm import scoped_session, sessionmaker
import sys
from xunkemgmt_client.market.feed_stats_v2.model.feed_stats import FeedStatsIndex
from xunkemgmt_client.market.feed_stats_v2.database.connection.coin2_database import coin2_feed_stats_importer_engine,coin2_feed_stats_querier_engine
from sqlalchemy import MetaData

FLAGS = flags.FLAGS

metadata = MetaData()
metadata.reflect(coin2_feed_stats_querier_engine)
FeedStatsInfo = metadata.tables['FeedStatsInfo']
CrossStats = metadata.tables['CrossStats']
BookStats = metadata.tables['BookStats']
IntervalStats = metadata.tables['IntervalStats']

machine_list = [
    # 'feed-01.ap-southeast-1.aws',
    # 'feed-01.ap-northeast-1.aws.huobi',
    # 'feed-02.ap-northeast-2.aws',
    # 'feed-02.eu-west-1.aws',
    # 'feed-02.us-east-1.aws',
    'feed-05.cn-hongkong.aliyun',
    # 'feed-10.ap-northeast-1.aws',
    'feed-05.ap-northeast-1.aws',
]


class FeedStatsReader():
  def __init__(self):
    self._session = self._get_session()

  def read(self, trading_date):
    # print(f"checking {trading_date}")
    session = self._session
    feed_stats_info = []
    for machine in machine_list:
      machine_feed_stats_info = pd.DataFrame(session.query(FeedStatsInfo).filter(
          FeedStatsInfo.c.machine == machine,
          FeedStatsInfo.c.trading_date == trading_date,
          FeedStatsInfo.c.feed_source == 'fastfeed',
          FeedStatsInfo.c.exchange_api_id.in_([87]),
          FeedStatsInfo.c.symbol == 'BTC-USDT.PERPETUAL',
          FeedStatsInfo.c.recipe == 'realtime_move2bp'
      ))
      if not machine_feed_stats_info.empty:
        feed_stats_info.append(machine_feed_stats_info)
    if (len(feed_stats_info) > 0):
      feed_stats_info = pd.concat(feed_stats_info, axis=0)
      feed_stats_info.sort_values(by='create_time', inplace=True)
      hold_feed_stats = feed_stats_info.drop_duplicates(
          ['trading_date', 'machine', 'exchange_api_id', 'symbol', 'native_symbol', 'worker', 'feed_source', 'recipe'], keep='first')
    if len(feed_stats_info) > 0:
      machine_cross_stats_info = pd.DataFrame(session.query(CrossStats).filter(CrossStats.c.feed_stats_id.in_(hold_feed_stats['feed_stats_id']),))
      machine_book_stats_info = pd.DataFrame(session.query(BookStats).filter(BookStats.c.feed_stats_id.in_(hold_feed_stats['feed_stats_id']),))

      # #latency stats summary
      # latency_stats_df = machine_cross_stats_info.set_index('feed_stats_id').join(hold_feed_stats.set_index('feed_stats_id'), on='feed_stats_id',how='inner')
      # latency_stats_df = latency_stats_df[['trading_date', 'exchange_api_id', 'worker', 'symbol','avg_message_time_diff', 'max_message_time_diff']]
      # print(latency_stats_df.groupby(by=['worker','exchange_api_id','symbol']).mean())
      # # book count compare
      # bkcount_stats_df = machine_book_stats_info.set_index('feed_stats_id').join(hold_feed_stats.set_index('feed_stats_id'), on='feed_stats_id',how='inner')
      # book_count_05 = int(bkcount_stats_df[bkcount_stats_df['machine'] == 'feed-05.ap-northeast-1.aws']['book_count'])
      # book_count_other = list(bkcount_stats_df[bkcount_stats_df['machine'] != 'feed-05.cn-hongkong.aliyun']['book_count'])

      


  def _get_session(self):
    return scoped_session(sessionmaker(autocommit=False,
                                       autoflush=False,
                                       bind=coin2_feed_stats_importer_engine))

def main(_):
  reader = FeedStatsReader()
  # date = datetime.datetime.strptime('20230610', '%Y%m%d')
  start = datetime.date(2023,3,31)
  end = datetime.date(2023,7,28)
  while start < end:
    reader.read(start)
    start += datetime.timedelta(days=1)
  
if __name__ == '__main__':
  main(None)